﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DWH.Utils;
using System.IO;
using System.Drawing;
using log4net;
using DWH.Data;

namespace DWH.PP.LoopUrls.AICH
{
    public class AICHSavePageAttachmentTask : PPLoopUrlsBoardPullTask
    {
        private ILog logger;

        public AICHSavePageAttachmentTask()
        {
            logger = LogManager.GetLogger(typeof(AICHSavePageAttachmentTask));
        }

        public override string ProcessTargetHtmlContent(string pageContentUrl, string targetHtmlContent)
        {
            if (!String.IsNullOrEmpty(attachmentStoreFolder))
            {
                string attFolderForThisPage = Path.Combine(attachmentStoreFolder, pageContentUrl.GetHashCode().ToString());
                //if (!Directory.Exists(attFolderForThisPage))
                //{
                //    Directory.CreateDirectory(attFolderForThisPage);
                //}
                //move download attachment to another task, skip the foder creation

                List<string> torrentCans = DWHHelper.ExtractPatternFromStringContent(targetHtmlContent, DWHConstants.REGEX_HTML_A, DWHConstants.REGEX_HTML_A_GROUP_LINKURL);

                foreach (string linkUrl in torrentCans)
                {
                    if (linkUrl.Contains(DWHConstants.FLAG_TORRENT_JANDOWN))
                    {
                        DWHDBHelper.AddUndownloadedPageAttachment(linkUrl, pageContentUrl.GetHashCode(),
                           DWHConstants.ATTACHMENT_TYPE_TORRENT_JIANDOWN,
                            //String.Format("Torrent-{0}", DWHConstants.FLAG_TORRENT_JANDOWN),
                           attFolderForThisPage, DateTime.Now);

                        //try
                        //{
                        //    DWHHelper.DownloadTorrentJandown(linkUrl, attFolderForThisPage);
                        //}
                        //catch (Exception ex)
                        //{
                        //    logger.ErrorFormat("error occors when trying to download torrent from jandown.com, url:{0} err:{1}", linkUrl, ex.ToString());
                        //}
                        continue;

                    }

                    if (linkUrl.Contains(DWHConstants.FLAG_TORRENT_UHOPEFIND))
                    {
                        DWHDBHelper.AddUndownloadedPageAttachment(linkUrl, pageContentUrl.GetHashCode(),
                           DWHConstants.ATTACHMENT_TYPE_TORRENT_UHOPEFIND,
                            // String.Format("Torrent-{0}", DWHConstants.FLAG_TORRENT_UHOPEFIND),
                           attFolderForThisPage, DateTime.Now);

                        //try
                        //{
                        //    DWHHelper.DownloadTorrentUhopefind(linkUrl, attFolderForThisPage);
                        //}
                        //catch (Exception ex)
                        //{
                        //    logger.ErrorFormat("error occors when trying to download torrent from uhopefind.info, url:{0} err:{1}", linkUrl, ex.ToString());
                        //}
                        continue;
                    }
                }

                List<string> images = DWHHelper.ExtractPatternFromStringContent(targetHtmlContent,
                    DWHConstants.REGEX_HTML_IMG, DWHConstants.REGEX_HTML_IMG_GROUP_IMGURL);

                DWHDBHelper.AddUndownloadedPageAttachments(images, pageContentUrl.GetHashCode(),
                    DWHConstants.ATTACHMENT_TYPE_IMAGE,
                    //"Image",
                    attFolderForThisPage, DateTime.Now);

                //DWHHelper.DownloadWebPageImages(TaskName, attFolderForThisPage, images);

            }

            return targetHtmlContent;
        }
    }
}
